package com.jy.appmarket;

import java.io.File;

import com.android.corestat.utils.CSPrefs;
import com.android.corestat.utils.SimUtil;
import com.jy.app.market.idata.data.Login;
import com.jy.appmarket.download.DownloadService;
import com.jy.appmarket.screenlock.LockSoundManager;
import com.jy.appmarket.screenlock.service.LockerService;
import com.jy.appmarket.screenlock.utils.PatternLockerUtils;
import com.jy.appmarket.utils.LogUtil;
import com.jy.appmarket.utils.LoginUtil;
import com.jy.appmarket.utils.StorageConfig;
import com.jy.appmarket.utils.WidgetIconUtils;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.rad.datastat.service.DatastatService;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import cn.jpush.android.api.JPushInterface;

/**
 * 类说明：
 * 
 * @date 2013-4-30
 * @version 1.0
 */
public class AppMarketApplication extends Application {
	protected static final String TAG = AppMarketApplication.class.getSimpleName();

	private static AppMarketApplication app;

	private boolean mAppIsRunning = false;

	public static boolean showLocal = true;
	public static boolean appUpdateNotificationShowing = false;
	private static Login mLogin;
	// 判断是否是在有秘密锁的状态下使用相机和手电筒
	public static boolean sIsPauseLocked;
	private static PatternLockerUtils mLockPatternUtils;

	@Override
	public void onCreate() {
		super.onCreate();
		app = this;
		StorageConfig.setCacheDir(this);
		WidgetIconUtils.getInstance().initDefaultPackage(this);
		// 设置开启日志,发布时请关闭日志
		// JPushInterface.setDebugMode(true);
		JPushInterface.init(this);
		initImageLoader();
		startService(new Intent(getApplicationContext(), DownloadService.class));

		CSPrefs.setElapsedRealTime(this, SystemClock.elapsedRealtime());
		CSPrefs.setSimState(this, SimUtil.isSimIn(this));
		startService(new Intent(getApplicationContext(), DatastatService.class));

		if (LoginUtil.isLogin(this)) {
			mLogin = LoginUtil.getLogin(this);
		}
		mLockPatternUtils = new PatternLockerUtils(this);
		LockSoundManager.initSoundPool(this);
		LockerService.startService(this);
	}

	private void initImageLoader() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
				.resetViewBeforeLoading(false).displayer(new FadeInBitmapDisplayer(100)).build();

		ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(getApplicationContext());
		config.threadPriority(Thread.NORM_PRIORITY - 1);
		config.memoryCacheSizePercentage(50);
		config.diskCache(new UnlimitedDiskCache(new File(StorageConfig.CACHE_DIR)));
		config.diskCacheFileNameGenerator(new HashCodeFileNameGenerator());
		config.diskCacheSize(100 * 1024 * 1024); // 100 MiB
		config.tasksProcessingOrder(QueueProcessingType.LIFO);
		config.defaultDisplayImageOptions(options);
		// config.writeDebugLogs(); // Remove for release app
		ImageLoader.getInstance().init(config.build());
	}

	public void setAppIsRunning(boolean isRun) {
		mAppIsRunning = isRun;
	}

	public boolean isAppRunning() {
		return mAppIsRunning;
	}

	public static AppMarketApplication getApp() {
		return app;
	}

	public static Login getLogin() {
		return mLogin;
	}

	public static void setLogin(Login login) {
		mLogin = login;
	}

	public static boolean isLogin() {
		if (mLogin == null) {
			return false;
		} else {
			return !TextUtils.isEmpty(mLogin.getUserId());
		}
	}

	public static PatternLockerUtils getLockPatternUtils() {
		return mLockPatternUtils;
	}
}
